﻿Imports System.Data.SqlClient
Public Class SuaPhieuThue
    Dim cnStr As String = "Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True"
    Dim con As New SqlConnection(cnstr)
    Private Sub SuaPhieuThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadGV()
        loadcbx()
    End Sub
    Private Sub loadGV()
        Dim sql As String = "select MaPhieu,TenPhong,TenKhach,TenLoaiKhach,CMND,DiaChi,NgayBatDau,SoKhach,p.MaLoaiKhach as MaLoaiKhach from PhieuThue p,LoaiKhach l where l.MaLoaiKhach=p.MaLoaiKhach"
        Dim bs As New BindingSource()
        Dim da As New SqlDataAdapter(sql, con)
        Dim dt As New DataTable()
        da.Fill(dt)
        bs.DataSource = dt
        DataGridView1.DataSource = bs
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub loadcbx()
        Dim con As New SqlConnection(cnStr)
        Dim sql As String = "select * from LoaiKhach"
        Dim da As New SqlDataAdapter(sql, con)
        Dim dt As New DataTable()
        da.Fill(dt)
        cbxLoaiK.DataSource = dt
        cbxLoaiK.DisplayMember = "TenLoaiKhach"
        cbxLoaiK.ValueMember = "MaLoaiKhach"
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If (e.RowIndex >= 0 And e.ColumnIndex >= 0 And e.ColumnIndex <> Nothing) Then
            txtTenKhach.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("TenKhach").Value)
            TxtCMND.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("CMND").Value)
            txtDiaChi.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("DiaChi").Value)
            txtTenP.Text = Convert.ToString(DataGridView1.CurrentRow.Cells("TenPhong").Value)
            DateTimePicker1.Value = Convert.ToDateTime(DataGridView1.CurrentRow.Cells("NgayBatDau").Value)
            cbxLoaiK.SelectedValue = DataGridView1.CurrentRow.Cells("MaLoaiKhach").Value
            txtslg.Text = DataGridView1.CurrentRow.Cells("SoKhach").Value
        End If
    End Sub


    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If (txtTenP.Text <> "") Then


            If MessageBox.Show("Chắc chắn chưa?", "Thông Báo", MessageBoxButtons.OKCancel, _
                               Nothing, MessageBoxDefaultButton.Button1) = DialogResult.OK Then
                If (txtTenKhach.Text <> "" And txtTenP.Text <> "" And TxtCMND.Text <> "" And txtDiaChi.Text <> "") Then
                    Dim sql As String = "update PhieuThue set TenPhong=@TenPhong,TenKhach=@TenKhach,CMND=@CMND,DiaChi=@DiaChi,MaLoaiKhach=@MaLoaiKhach,NgayBatDau=@NgayBatDau,SoKhach=@SoKhach where MaPhieu=@MaPhieu"
                    Dim command As New SqlCommand(sql, con)

                    command.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 40)
                    command.Parameters("@TenPhong").Value = txtTenP.Text

                    command.Parameters.Add("@TenKhach", SqlDbType.NVarChar, 40)
                    command.Parameters("@TenKhach").Value = txtTenKhach.Text

                    command.Parameters.Add("@CMND", SqlDbType.Int, 9)
                    command.Parameters("@CMND").Value = TxtCMND.Text

                    command.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50)
                    command.Parameters("@DiaChi").Value = txtDiaChi.Text

                    command.Parameters.Add("@MaLoaiKhach", SqlDbType.Int, 50)
                    command.Parameters("@MaLoaiKhach").Value = cbxLoaiK.SelectedValue

                    command.Parameters.Add("@NgayBatDau", SqlDbType.Date, 40)
                    command.Parameters("@NgayBatDau").Value = DateTimePicker1.Value

                    command.Parameters.Add("@SoKhach", SqlDbType.Int, 10)
                    command.Parameters("@SoKhach").Value = txtslg.Text

                    command.Parameters.Add("@MaPhieu", SqlDbType.Int, 10)
                    command.Parameters("@MaPhieu").Value = DataGridView1.CurrentRow.Cells("MaPhieu").Value


                    command.Connection.Open()
                    Dim i As Integer = command.ExecuteNonQuery() 'thuc thi lenh
                    If (i > 0) Then
                        MessageBox.Show("Đã Sửa Thông Tin Phiếu Thuê")
                        cleartxt()

                    Else
                        MessageBox.Show("Xảy Ra Lỗi")
                    End If
                    command.Connection.Close()
                    loadGV()
                Else
                    MessageBox.Show("Chưa Chọn Thông Tin Cần Sửa")
                End If
            End If
        End If
    End Sub

    Private Sub cleartxt()
        TxtCMND.Text = ""
        txtDiaChi.Text = ""
        TxtCMND.Text = ""
        txtTenP.Text = ""
        txtTenKhach.Text = ""
    End Sub

    Private Sub TxtCMND_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCMND.TextChanged
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub


    Private Sub txtslg_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtslg.MouseClick
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub

    Private Sub txtTenKhach_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTenKhach.MouseClick
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub

    Private Sub txtDiaChi_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtDiaChi.MouseClick
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub

    Private Sub txtTenP_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTenP.MouseClick
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub

    Private Sub cbxLoaiK_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbxLoaiK.MouseClick
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub

    Private Sub DateTimePicker1_MouseCaptureChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.MouseCaptureChanged
        If TxtCMND.Text.Length <> 9 Then
            MessageBox.Show("CMND phải đủ 9 ký tự!")
            TxtCMND.Focus()
        End If
    End Sub

   

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New SqlConnection(cnStr)
        Dim sql As String = "delete from PhieuThue where MaPhieu=@MaPhieu"
        Dim cmd As New SqlCommand(sql, con)

        cmd.Parameters.Add("@MaPhieu", SqlDbType.Int, 10)
        cmd.Parameters("@MaPhieu").Value = DataGridView1.CurrentRow.Cells("MaPhieu").Value
        cmd.Connection.Open()
        Dim i As Integer = cmd.ExecuteNonQuery() 'thuc thi lenh
        If (i > 0) Then
            MessageBox.Show("Đã xóa phiếu")
        Else
            MessageBox.Show("Xảy ra lỗi!!")
            loadGV()
        End If
        cmd.Connection.Close()
        DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

End Class